package com.channel.temperature.collect.ui.activity

import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.StrictMode
import androidx.core.content.FileProvider
import com.hjq.toast.ToastUtils
import com.channel.temperature.collect.R
import com.channel.temperature.collect.app.base.BaseActivity
import com.channel.temperature.collect.app.ext.initBack
import com.channel.temperature.collect.databinding.ActivityPdfBinding
import com.channel.temperature.collect.ui.viewmodel.PdfModel
import me.hgj.mvvmhelper.ext.setOnclickNoRepeat
import java.io.File


class PdfActivity : BaseActivity<PdfModel, ActivityPdfBinding>() {
    var filePath: String = ""
    override fun initView(savedInstanceState: Bundle?) {
        mToolbar.initBack("本地PDF报表") {
            finish()
        }
        mToolbar.setRightDrawable(R.mipmap.icon_share)
        filePath = intent.getStringExtra("filePath").toString()
        mBind.pdfView.fromFile(File(filePath)).load()
    }

    /**
     * 请求成功
     */
    override fun onRequestSuccess() {
    }

    override fun onBindViewClick() {
        setOnclickNoRepeat(
            mToolbar.getRightId(),
        ) {
            when (it) {
                mToolbar.getRightId() -> {
                    val builder = StrictMode.VmPolicy.Builder()
                    StrictMode.setVmPolicy(builder.build())
                    builder.detectFileUriExposure()
                    val intent = Intent(Intent.ACTION_SEND)
                    var currentapiVersion = android.os.Build.VERSION.SDK_INT;
                    if (currentapiVersion >= 24) {
                        val uri = FileProvider.getUriForFile(
                            this.getApplicationContext(),
                            "com.tazan.temperature.collect.provider",
                            File(filePath)
                        );
                        intent.putExtra(Intent.EXTRA_STREAM, uri)
                        intent.type = "application/octet-stream"
                        intent.addCategory(Intent.CATEGORY_DEFAULT);
                    } else {
                        val uri = Uri.fromFile(File(filePath));
                        intent.putExtra(Intent.EXTRA_STREAM, uri)
                        intent.type = "application/octet-stream"
                        intent.addCategory(Intent.CATEGORY_DEFAULT);
                    }
                    try {
                        startActivity(intent)
                    } catch (exception: ActivityNotFoundException) {
                        ToastUtils.show("暂无分享目标应用")
                    }

                }
            }
        }
    }


}